home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / muds / pennmush.000 / pennmush-1.50-p8-linux.tar / pennmush / stringutil.c < prev    next >
C/C++ Source or Header  |  1993-04-16  |  4KB  |  230 lines

  1. /* stringutil.c */
  2.  
  3. /* String utilities */
  4.  
  5. #include <ctype.h>
  6. #include <string.h>
  7.  
  8. #include "copyright.h"
  9. #include "config.h"
  10. #include "interface.h"
  11. #include "globals.h"
  12.  
  13. #ifdef NEVER
  14. int strcasecmp(s1, s2)
  15.     const char *s1;
  16.     const char *s2;
  17. {
  18.   while (*s1 && *s2 && DOWNCASE(*s1) == DOWNCASE(*s2))
  19.     s1++, s2++;
  20.  
  21.   return (DOWNCASE(*s1) - DOWNCASE(*s2));
  22. }
  23. #endif                /* NEVER */
  24.  
  25. int string_prefix(string, prefix)
  26.     const char *string;
  27.     const char *prefix;
  28. {
  29.   if (!string || !prefix) return 0;
  30.   while (*string && *prefix && DOWNCASE(*string) == DOWNCASE(*prefix))
  31.     string++, prefix++;
  32.   return *prefix == '\0';
  33. }
  34.  
  35. /* accepts only nonempty matches starting at the beginning of a word */
  36. const char *string_match(src, sub)
  37.     const char *src;
  38.     const char *sub;
  39. {
  40.     if (!src || !sub)
  41.     return 0;
  42.  
  43.     if (*sub != '\0') {
  44.     while (*src) {
  45.         if (string_prefix(src, sub))
  46.         return src;
  47.         /* else scan to beginning of next word */
  48.         while (*src && isalnum(*src))
  49.         src++;
  50.         while (*src && !isalnum(*src))
  51.         src++;
  52.     }
  53.     }
  54.     return 0;
  55. }
  56.  
  57. char *strupper(s)
  58.     const char *s;
  59. {
  60.     static char buf1[BUFFER_LEN];
  61.     char *p;
  62.  
  63.     strcpy(buf1, s);
  64.     for(p = buf1; *p; p++)
  65.       *p = UPCASE(*p);
  66.     return buf1;
  67. }
  68.  
  69. char *upcasestr(s)
  70.      char *s;
  71. {
  72.   /* modifies a string in-place to be upper-case */
  73.  
  74.   char *p;
  75.   for (p = s; p && *p; p++)
  76.     *p = UPCASE(*p);
  77.   return s;
  78. }
  79.  
  80. /* safe_chr and safe_str are essentially straight out of the 2.0 code */
  81.  
  82. int safe_chr(c, buf, bufp)
  83.      char c;
  84.      char *buf;
  85.      char **bufp;
  86. {
  87.   /* adds a character to a string, being careful not to overflow buffer */
  88.  
  89.   char *tp;
  90.   int val;
  91.  
  92.   tp = *bufp;
  93.   val = 0;
  94.  
  95.   if ((tp - buf) < BUFFER_LEN)
  96.     *tp++ = c;
  97.   else 
  98.     val = 1;
  99.  
  100.   *bufp = tp;
  101.   return val;
  102. }
  103.  
  104. int safe_copy_str(c, buff, bp, maxlen)
  105.      char *c;
  106.      char *buff;
  107.      char **bp;
  108.      int maxlen;
  109. {
  110.   /* copies a string into a buffer, making sure there's no overflow. */
  111.  
  112.   char *tp;
  113.  
  114.   tp = *bp;
  115.   if (c == NULL)
  116.     return 0;
  117.   while (*c && ((tp - buff) < maxlen))
  118.     *tp++ = *c++;
  119.   *bp = tp;
  120.   return strlen(c);
  121. }
  122.  
  123. /* skip_space and seek_char are essentially right out of the 2.0 code */
  124.  
  125. char *skip_space(s)
  126.      const char *s;
  127. {
  128.   /* returns pointer to the next non-space char in s, or NULL if s == NULL
  129.    * or *s == NULL or s has only spaces.
  130.    */
  131.  
  132.   char *c = (char *) s;
  133.   while (c && *c && isspace(*c))
  134.     c++;
  135.   return c;
  136. }
  137.  
  138. char *seek_char(s, c)
  139.      const char *s;
  140.      char c;
  141. {
  142.   /* similar to strchr(). returns a pointer to the next char in s which
  143.    * matches c, or a pointer to the terminating null at the end of s.
  144.    */
  145.  
  146.   char *p = (char *) s;
  147.   while (p && *p && (*p != c))
  148.     p++;
  149.   return p;
  150. }
  151.  
  152. #ifdef NEED_STRDUP
  153.  
  154. #ifdef NEVER
  155. char *strdup(s)
  156.      const char *s;
  157. {
  158.   char *dup;
  159.   
  160.   dup = (char *) malloc(strlen(s) + 1);
  161.   strcpy(dup, s);
  162.   return dup;
  163. }
  164.  
  165. #else
  166.  
  167. char *strdup(s)
  168.      const char *s;
  169. {
  170.   int len;
  171.   int dup = 0;
  172.  
  173.   len = strlen(s) + 1;
  174.   if ((dup = (char *)malloc(len)) != NULL)
  175.     bcopy(s, dup, len);
  176.   return (dup);
  177. }
  178. #endif
  179.  
  180. #endif                /* NEED_STRDUP */
  181.  
  182. char *replace_string(old, new, string)
  183.      const char *old;
  184.      const char *new;
  185.      const char *string;
  186. {
  187.   /* another 2.0 function: replaces string "old" with string "new".
  188.    * The result returned by this must be freed.
  189.    */
  190.  
  191.   char *result, *r, *s;
  192.   int len;
  193.  
  194.   if (!string)
  195.     return NULL;
  196.  
  197.   s = (char *) string;
  198.   len = strlen(old);
  199.   r = result = (char *) malloc(BUFFER_LEN + 1);
  200. #ifdef MEM_CHECK
  201.   add_check("replace_string.buff");
  202. #endif
  203.  
  204.   while (*s) {
  205.  
  206.     /* copy up to the next occurence of first char of old */
  207.     while (*s && *s != *old) {
  208.       safe_chr(*s, result, &r);
  209.       s++;
  210.     }
  211.  
  212.     /* if we've really found  old, append new to the result and
  213.      * move past the occurence of old. Else, copy the char and
  214.      * continue.
  215.      */
  216.     if (*s) {
  217.       if (!strncmp(old, s, len)) {
  218.     safe_copy_str((char *) new, result, &r, BUFFER_LEN);
  219.     s += len;
  220.       } else {
  221.     safe_chr(*s, result, &r);
  222.     s++;
  223.       }
  224.     }
  225.   }
  226.  
  227.   *r = '\0';
  228.   return result;
  229. }
  230.